package com.bd.framework.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.bd.framework.pojo.IniInfo;

/**
 * @author hnxdyl@hotmail.com
 * 解析ini文件,并返回iniInfo对象
 * 配置ini文件,并返回保存结果。
 * */
public class IniConfig {
	private IniEditor iniReader = new IniEditor();
	private String iniPath = System.getProperty("user.dir").replace('\\', '/')+"/src/conf/wncupos.ini"; //ini 文件的路径
	
	public Map<String, Object> getIniInfo(){
		String erroInfo = "";
		IniInfo iniInfo = new IniInfo();
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			File file = new File(iniPath);
			iniReader.load(file);
			iniInfo.setMainIp(iniReader.get("shop", "mainip"));
			iniInfo.setBackupIp(iniReader.get("shop", "backupip"));
			iniInfo.setShopCode(iniReader.get("shop", "shopcode"));
			iniInfo.setServerId(iniReader.get("shop", "serverid"));
			iniInfo.setDynamic(iniReader.get("shop", "dynamic"));
			iniInfo.setDownTime(iniReader.get("shop", "downtime"));
			iniInfo.setDownTimes(iniReader.get("shop", "downtimes"));
			iniInfo.setOffLine(iniReader.get("shop", "offline"));
			iniInfo.setPoint(iniReader.get("shop", "point"));
			iniInfo.setBhFlag(iniReader.get("shop", "bhflag"));
			iniInfo.setCkFlag(iniReader.get("shop", "ckflag"));
			iniInfo.setBillNum(iniReader.get("shop", "billnum"));
			erroInfo = "success";
		} catch (FileNotFoundException e) {
			erroInfo = "找不到该文件";
		} catch (IOException e) {
			erroInfo = "文件读取错误";
		}
		if(erroInfo == "success" || erroInfo.equals("success")){
			map.put("success", iniInfo);
		}else{
			map.put("error", erroInfo);
		}
		return map;
	}
	
	public String saveIniInfo(IniInfo iniInfo){
		String erroInfo = "";
		iniReader.set("shop", "mainip", iniInfo.getMainIp());
		iniReader.set("shop", "backupip", iniInfo.getBackupIp());
		iniReader.set("shop", "shopcode", iniInfo.getShopCode());
		iniReader.set("shop", "serverid", iniInfo.getServerId());
		iniReader.set("shop", "dynamic", iniInfo.getDynamic());
		iniReader.set("shop", "downtime", iniInfo.getDownTime());
		iniReader.set("shop", "downtimes", iniInfo.getDownTimes());
		iniReader.set("shop", "offline", iniInfo.getOffLine());
		iniReader.set("shop", "point", iniInfo.getPoint());
		iniReader.set("shop", "bhflag", iniInfo.getBhFlag());
		iniReader.set("shop", "ckflag", iniInfo.getCkFlag());
		iniReader.set("shop", "billnum", iniInfo.getBillNum());
		 try {
			 iniReader.save(new File(iniPath));
			 erroInfo = "保存成功!";
	        } catch (Exception e1) {          
	          e1.printStackTrace();
	          erroInfo = "保存信息出错!";
	     }
		return erroInfo;
	}
}
